﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XNode;

[NodeWidth(200)]
public class DialogNode : MyXNode
{
    public static string GetDefaultName(Type type)
    {
        if (type == typeof(DialogNode))
            return "空节点";
        if (type == typeof(DialogNode_Select))
            return "分支节点";
        if (type == typeof(DialogNode_Sequential))
            return "顺序对话";
        if (type == typeof(DialogNode_ChangeIndex))
            return "修改起始索引";
        return null;
    }

    protected DialogGraph MyDialogGraph => graph as DialogGraph;
    public int index;

    [Output]
    public DialogNode self;
    [Input(backingValue = ShowBackingValue.Always)]
    public DialogNode next;

    public override object Output(NodePort port)
    {
        if (port.fieldName == nameof(self))
            return this;
        return base.Output(port);
    }

    public override void OnCreateConnection(NodePort from, NodePort to)
    {
        base.OnCreateConnection(from, to);
        DialogNode f = from.node as DialogNode;
        DialogNode t = to.node as DialogNode;
        if (f.index <= t.index)
            f.index = t.index + 1;
        MyDialogGraph.Adjust();
    }

    public virtual void Visit(DialogSystem dialogSystem) { }

    public class Comparer : IComparer<DialogNode>, IComparer<Node>
    {
        public int Compare(DialogNode x, DialogNode y)
        {
            if (x == null || y == null)
                return 0;
            return x.index - y.index;
        }

        public int Compare(Node x, Node y)
        {
            return Compare(x as DialogNode, y as DialogNode);
        }
    }
}